package com.example.tetris.gui;

import javafx.animation.*;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.util.Duration;

public class AnimationGenerator {
    public static final int cols = GamePageController.cols;
    public static Timeline blinkLineAnimation(Timeline timeline, Rectangle[][] rectangles, int... rowIndex){
        final int animationDuration = 2000;
        timeline.pause(); //原有的基础时钟暂停2s
        PauseTransition pauseTransition = new PauseTransition(new Duration(animationDuration));
        pauseTransition.setOnFinished(e->timeline.play());
        pauseTransition.play();
        Timeline animationTimeLine = null;
        for (int y : rowIndex) {
            for (int x = 0; x < cols; x++) {
                animationTimeLine = blinkRectangleAnimation(x,y,animationDuration,rectangles);
            }
        }
        return animationTimeLine;

    }
    private  static Timeline blinkRectangleAnimation(int x, int y, int animationDuration, Rectangle[][] rectangles){
        Rectangle rectangle = rectangles[y - 2][x];
        final int blinkPeriod = 400;

        Timeline timelineBlink = new Timeline(
                new KeyFrame(Duration.ZERO, new KeyValue(rectangle.fillProperty(), Color.RED)),
                new KeyFrame(Duration.millis(blinkPeriod), new KeyValue(rectangle.fillProperty(), Color.PINK)),
                new KeyFrame(Duration.millis(2 * blinkPeriod), new KeyValue(rectangle.fillProperty(), Color.RED))
        );

        timelineBlink.play();
        timelineBlink.setCycleCount(animationDuration / blinkPeriod);

        return timelineBlink;

    }

    public static void scaleLabel(Label label, double scaleUpperBound){

        // 创建 ScaleTransition 对象，设置节点和动画时长
        ScaleTransition scaleTransition = new ScaleTransition(Duration.seconds(0.4), label);
        // 设置初始缩放比例
        scaleTransition.setFromX(1.0);
        scaleTransition.setFromY(1.0);

        // 设置结束缩放比例
        scaleTransition.setToX(scaleUpperBound);
        scaleTransition.setToY(scaleUpperBound);

        // 设置自动反向播放（从大变小再从小变大）
        scaleTransition.setAutoReverse(true);
        scaleTransition.setCycleCount(2);
        // 播放动画
        scaleTransition.play();
    }
}
